package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.LinkSpuProductBean;
import com.ymatou.iapi.productmanager.parameter.ProductCatalogDto;
import com.ymatou.iapi.productmanager.parameter.ProductCatalogDtoV2;
import com.ymatou.iapi.productmanager.parameter.ProductCategoryDto;
import com.ymatou.iapi.productmanager.parameter.SpuCataLogBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetProductAndCatalogInfoCall;
import com.ymatou.iapi.productmanager.service.LinkSpuProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
/**
*关联商品库Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LinkSpuProduct{
	private static LinkSpuProductBean linkspuproductBean;
	private static LinkSpuProductCall linkspuproductCall;
	
	private static Integer sellerId = 3383;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("关联商品库Test");
	}

	@Before
	public void caseUp() {
		linkspuproductBean=new LinkSpuProductBean();
		linkspuproductCall=new LinkSpuProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(linkspuproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LinkSpuProduct_001() {
		Logger.start(true,"新建商品关联商品库，成功");
		try {
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerSpuProduct(sellerId);
			addProdReq.getProduct().setSpuId(0);
			addProdReq.getProduct().setSpuProductId(null);;
			addProdReq.getProduct().getCatalogList().forEach(v->{v.setSpuCatalogId(null);});
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			
			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			linkspuproductBean.setSellerId(sellerId);
			linkspuproductBean.setProductId(productId);
			linkspuproductBean.setUseSpuProductDesc(true);
			linkspuproductBean.setSpuProductId(229);
			List<SpuCataLogBean> cataLogs = new ArrayList<SpuCataLogBean>();
			cataLogs.add(new SpuCataLogBean(catalogIds.get(0), 1250));
			cataLogs.add(new SpuCataLogBean(catalogIds.get(1), 1249));
			linkspuproductBean.setCatalogList(cataLogs);
			linkspuproductCall.setData(linkspuproductBean);
			linkspuproductCall.callService();	
			
			Logger.verifyEquals("200", linkspuproductCall.getString("Code"), "验证code");
			
			JSONObject ret = new GetProductAndCatalogInfoCall().callAndGetReturnData(sellerId, productId);
//			Logger.verifyEquals(229,ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct").getInt("SpuId"),"验证SpuId");
			Logger.verifyEquals(229,ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct").getInt("SpuProductId"),"验证SpuProductId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}